#include "musicplaybar.h"
#include "ui_musicplaybar.h"
#include <QEventLoop>
#include<QThread>
#include<QFile>
#include"loadmusic.h"
int index=0;
QString changetime(QString time)
{
    int seconds = time.toInt();
    int minutes = (seconds % 3600) / 60;
    int remainingSeconds = seconds % 60;

    QString formattedTime = QString("%2:%3")
        .arg(minutes, 2, 10, QLatin1Char('0'))
        .arg(remainingSeconds, 2, 10, QLatin1Char('0'));


    return formattedTime;
}

MusicPlayBar::MusicPlayBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicPlayBar)
{
    ui->setupUi(this);
    init();
    m_music=new QMediaPlayer(this);;  //背景音乐
    path.append("./song/");


    connect(this,&MusicPlayBar::play,this,&MusicPlayBar::setPath);



}

MusicPlayBar::~MusicPlayBar()
{
    delete ui;
}

void MusicPlayBar::setPath(QString musicpath)
{
    QString mp;
    mp.append(path);
    mp.append(musicpath).append(".mp3");
    m_music->setMedia(QUrl::fromLocalFile(mp));

    //播放前准备工作


    m_music->play();
    prepare();
    playmusic();
}

void MusicPlayBar::SendRequst(tcpclient *client, QString title)
{
     Q_UNUSED(client)
     //判断这首歌在当前用户列表下有没有缓存
     if(ismusictitlecache(title)){
         emit play(title);
         return ;
     }
     stopmusic();
     if(prevlm!=nullptr)
     {
         //暂停音乐
         prevlm->closeload();  //关闭上一个采集的线程
     }
     loadmusic*Lm=new loadmusic(title);
     //prevlm=Lm;
     connect(Lm,&loadmusic::destory,this,[&](loadmusic*L){
          delete  L;
          Lm=nullptr;
     });
     std::thread*pa=new std::thread([&](loadmusic*L,QString title){
          //播放音乐
          L->run();
          prevlm=nullptr;
          emit play(title);
     },Lm,title);


    pa->detach();
    //setPath(title);



}

void MusicPlayBar::init()
{
    ui->standard->setpicture(":/new/prefix1/res/biaozun.png",
                             ":/new/prefix1/res/biaozun.png",
                             ":/new/prefix1/res/biaozun.png");

    ui->soundoffect->setpicture(":/new/prefix1/res/yinxiao.png",
                                ":/new/prefix1/res/yinxiao.png",
                                ":/new/prefix1/res/yinxiao.png");

    ui->voloum->setpicture(":/new/prefix1/res/yinliang.png",
                           ":/new/prefix1/res/yinliang.png",
                           ":/new/prefix1/res/yinliang.png");

    ui->listen->setpicture(":/new/prefix1/res/listen.png",
                           ":/new/prefix1/res/listen.png",
                           ":/new/prefix1/res/listen .png");

    ui->playlist->setpicture(":/new/prefix1/res/bofangliebiao.png",
                             ":/new/prefix1/res/bofangliebiao.png",
                             ":/new/prefix1/res/bofangliebiao.png");


    this->timer=new QTimer(this);
    timer->start(1000);


    this->m_currenttime="0";
    this->m_duration="200";


    ui->duration->setText(changetime(m_duration));
    ui->currenttime->setText(changetime(m_currenttime));


    ui->widget_3->setMaxValue(200);
    connect(timer,&QTimer::timeout,this,[=](){
          ui->widget_3->setCurrentValue(index);
          if(index==200){
              timer->stop();
          }
          QString str;
          ui->currenttime->setText(changetime(QString::number(index)));
          index++;
    });
    ui->bofang->setpicture(":/new/prefix1/res/mp2_2.png"
                           ,":/new/prefix1/res/mp2.png"
                           ,":/new/prefix1/res/mp2_2.png");

    ui->bofang->setFixedSize(45,45);
    ui->bofang->move(605,11);

    ui->prev->setpicture(":/new/prefix1/res/prev1.png"
                         ,":/new/prefix1/res/prev2.png"
                         ,":/new/prefix1/res/prev1.png");
    ui->prev->setFixedSize(35,35);
    ui->prev->move(540,16);

    ui->next->setpicture(":/new/prefix1/res/next1.png"
                         ,":/new/prefix1/res/next2.png"
                         ,":/new/prefix1/res/next1.png");
    ui->next->setFixedSize(35,35);
    ui->next->move(680,16);

    ui->playinorder->setpicture(":/new/prefix1/res/sunxu_1.png"
                                ,":/new/prefix1/res/sunxu.png"
                                ,":/new/prefix1/res/sunxu_1.png");
    ui->playinorder->setFixedSize(30,30);
    ui->playinorder->move(470,16);


    ui->gechi->setpicture(":/new/prefix1/res/gechi_1.png"
                          ,":/new/prefix1/res/gechi.png"
                          ,":/new/prefix1/res/gechi_1.png");
    ui->gechi->setFixedSize(30,30);
    ui->gechi->move(750,16);

}

void MusicPlayBar::playmusic()
{
    this->isbofang=true;
    on_bofang_clicked();
}

void MusicPlayBar::stopmusic()
{
    this->isbofang=false;
    on_bofang_clicked();
}

bool MusicPlayBar::ismusictitlecache(QString title)
{
    QString mp;
    mp.append(path);
    mp.append(title).append(".mp3");

    QFile file(mp);
    if (file.exists())
    {
        return true;   //存在
    }
    return false;      //不存在
}

void MusicPlayBar::prepare()
{
    //获取时长
    //获取音频总时长 有bug
//    connect(m_music,&QMediaPlayer::durationChanged,this,[&](qint64 duration){
//        QString str=changetime(QString::number(duration));
//        ui->duration->setText(str);
//    });
//    //获取到当前时长 有bug  需要重采样 将mp3 数据转换为原始的pcm 数据才能获取到
//    connect(m_music,&QMediaPlayer::positionChanged,this,[&](qint64 position){
//        QString str=changetime(QString::number(position));
//        ui->currenttime->setText(str);
//    });



}

void MusicPlayBar::on_bofang_clicked()
{
      this->isbofang=!this->isbofang;
      if(this->isbofang){
          ui->bofang->setpicture(":/new/prefix1/res/mp2_2.png"
                                 ,":/new/prefix1/res/mp2.png"
                                 ,":/new/prefix1/res/mp2_2.png");
          m_music->pause();
      }
      else
      {
          ui->bofang->setpicture(":/new/prefix1/res/mp_1.png"
                                 ,":/new/prefix1/res/mp1.png"
                                 ,":/new/prefix1/res/mp_1.png");
          m_music->play();
      }
}
